// Copyright (C) 2011 by Ivan Sialitski

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.


var store = function (key, value) {
    var TYPE_OBJECT = "object",
        ATTR_NAME = "data-uid",
        GET_SRC = "source",
        al = arguments.length,
        map = store.__map,
        uid, f, obj;

    if (al == 1) {
        // getting
        if (typeof key == TYPE_OBJECT) {
            return map[key[ATTR_NAME]];
        }
        return map[key];
    } else {
        if (typeof key == TYPE_OBJECT) {
            uid = key[ATTR_NAME];
            if (!uid) {
                uid = key[ATTR_NAME] = String(store.__counter++);
            }
            if (typeof value == TYPE_OBJECT) {
                // creating object
                f = function () {};
                f.prototype = value;
                obj = new f(); // inherit all props
                obj[store.SRC_PROPERTY_NAME] = key; // adding methods
                obj[GET_SRC] = store.__getter;
                map[uid] = obj;
                if (obj.init) { // if there's init() method,
                    obj.init(); // call it.
                }
                return obj;
            } else {
                
                map[uid] = value;
                return value;
            }
        } else {
            // suppose the key is a string
            map[key] = value;
        }
    }
};

store.SRC_PROPERTY_NAME = "_ _ _";

store.__map = {};

store.__counter = 1;

store.__getter = function () {
    return this[store.SRC_PROPERTY_NAME];
}
















